package com.dullwolf.service.impl;

import com.dullwolf.cmd.osu.OsuHomeCommand;
import com.dullwolf.cmd.other.RollCommand;
import com.dullwolf.model.Osu;
import com.dullwolf.pojo.CqMsg;
import com.dullwolf.service.BotService;
import com.dullwolf.utils.CmdUtil;
import com.jfinal.kit.StrKit;
import io.jboot.core.rpc.annotation.JbootrpcService;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dullwolf
 */
@JbootrpcService
public class BotServiceImpl implements BotService {

    private static final int MESSAGE_TYPE_PRIVATE = 0;
    private static final int MESSAGE_TYPE_GROUP = 1;
    private static final int MESSAGE_TYPE_DISCUSS = 2;


    @Override
    public void sendCqMsg(String type, CqMsg cqMsg) {
        if (StrKit.notBlank(type)) {
            String params = null, cmd = null;
            String msgData = cqMsg.getMessage().trim();
            if (msgData.startsWith("!") || msgData.startsWith("！") || msgData.startsWith("*")) {
                params = CmdUtil.getTrimParams(msgData);//原来的完整指令
                cmd = CmdUtil.getLowerParams(msgData.substring(1));//去除前缀后，指令小写
            }
            switch (type) {
                case "private":
                    handle(cmd, MESSAGE_TYPE_PRIVATE, cqMsg, params);
                    break;
                case "group":
                    handle(cmd, MESSAGE_TYPE_GROUP, cqMsg, params);
                    break;
                case "discuss":
                    handle(cmd, MESSAGE_TYPE_DISCUSS, cqMsg, params);
                    break;
                default:
                    break;
            }
        }

    }

    private void handle(String cmd, int type, CqMsg msg, String params) {
        switch (type) {
            case MESSAGE_TYPE_PRIVATE:
                switchCmd(cmd, msg.getUserId(), msg.getUserId(), type, params);
                break;
            case MESSAGE_TYPE_GROUP:
                switchCmd(cmd, msg.getUserId(), msg.getGroupId(), type, params);
                break;
            case MESSAGE_TYPE_DISCUSS:
                switchCmd(cmd, msg.getUserId(), msg.getGroupId(), type, params);
                break;
            default:
                break;
        }
    }


    private void switchCmd(String cmd, long userId, long id, int type, String params) {
        //第一个指令roll
        if(cmd.startsWith("roll")){
            new RollCommand().executeRoll(id, type);
        }
        //第二个指令，查询OSU 各个模式的成绩
        if (cmd.startsWith("home") || cmd.startsWith("mania") || cmd.startsWith("taiko") || cmd.startsWith("ctb")) {
            int mode = 0;
            int num = 0;
            if (cmd.contains("home")) {
                num = 4;
            } else if (cmd.contains("taiko")) {
                mode = 1;
                num = 5;
            } else if (cmd.contains("ctb")) {
                mode = 2;
                num = 3;
            } else if (cmd.contains("mania")) {
                mode = 3;
                num = 5;
            }
            new OsuHomeCommand().executeOsuHome(userId, id, type, params,num,mode);
        }
    }





}
